package com.zelin.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zelin.dao.ItemCatDao;
import com.zelin.entity.ItemCatEntity;
import com.zelin.sellergoods.service.ItemCatService;
import com.zelin.utils.PageUtils;
import com.zelin.utils.Query;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;


@Service
public class ItemCatServiceImpl extends ServiceImpl<ItemCatDao, ItemCatEntity> implements ItemCatService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ItemCatEntity> page = this.page(
                new Query<ItemCatEntity>().getPage(params),
                new QueryWrapper<ItemCatEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 功能: 根据父id查询分类列表
     * 参数:
     * 返回值: java.util.List<com.zelin.entity.ItemCatEntity>
     * 时间: 2021/7/27 16:48
     */
    @Override
    public List<ItemCatEntity> findByParentId(Long pid) {
        //1. 根据父id查询所有分类
        List<ItemCatEntity> itemCatEntities = baseMapper.selectList(new QueryWrapper<ItemCatEntity>()
                .eq("parent_id", pid));

        //2. 将所有的分类放到redis中
        for (ItemCatEntity entity : list()) {
            //以分类名称为key，以模板id为值放到redis中
            redisTemplate.boundHashOps("itemCats").put(entity.getName(),entity.getTypeId());
        }
        return itemCatEntities;
    }

    @Override
    public List<ItemCatEntity> findAll() {
        return this.list();
    }

}